package com.supreme.omsserver.config;

import com.supreme.omsserver.config.props.RedisProps;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class RedisConfig {

    @Resource
    private RedisProps redisProps;

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(connectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);

        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    private RedisConnectionFactory connectionFactory(int database, String host, int port, String password) {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPort(port);
        if (StringUtils.isNotBlank(password)) {
            configuration.setPassword(password);
        }
        configuration.setDatabase(database);

        GenericObjectPoolConfig<Object> genericObjectPoolConfig = new GenericObjectPoolConfig<>();
        genericObjectPoolConfig.setMaxIdle(redisProps.getLettuce().getPool().getMaxIdle());
        genericObjectPoolConfig.setMinIdle(redisProps.getLettuce().getPool().getMinIdle());
        genericObjectPoolConfig.setMaxTotal(redisProps.getLettuce().getPool().getMaxActive());
        genericObjectPoolConfig.setMaxWait(Duration.ofMillis(redisProps.getLettuce().getPool().getMaxWait()));

        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(redisProps.getTimeout()))
                .poolConfig(genericObjectPoolConfig)
                .build();

        LettuceConnectionFactory lettuce = new LettuceConnectionFactory(configuration, clientConfig);
        lettuce.afterPropertiesSet();
        return lettuce;
    }

    private RedisTemplate<String, Serializable> createRedisTemplate(int database) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        RedisConnectionFactory connectionFactory = this.connectionFactory(database, redisProps.getHost(), redisProps.getPort(), redisProps.getPassword());
        redisTemplate.setConnectionFactory(connectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);

        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    @Bean(name = "redisTemplate0")
    public RedisTemplate<String, Serializable> redisTemplate0() {
        return this.createRedisTemplate(1);
    }

    @Bean(name = "redisTemplate1")
    public RedisTemplate<String, Serializable> redisTemplate1() {
        return this.createRedisTemplate(1);
    }

    @Bean(name = "redisTemplateList")
    public List<RedisTemplate<String, Serializable>> redisTemplateList() {
        List<RedisTemplate<String, Serializable>> redisTemplateList = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            RedisTemplate<String, Serializable> redisTemplate = this.createRedisTemplate(i);
            redisTemplateList.add(redisTemplate);
        }

        return redisTemplateList;
    }
}
